<!--注册页面  -->
<template>
  <div class="register-card">
    <h3 class="register-title">Sign up</h3>
    <p class="register-desc">Hello, welcome to COVID-19 Vaccine fund</p>
    <div class="register-form">
      <div class="form-item">
        <input
          type="text"
          v-model="loginForm.phone"
          placeholder="Phone number"
        />
      </div>

      <div class="form-item">
        <input
          type="password"
          v-model="loginForm.password"
          placeholder="Password"
        />
      </div>
      <div class="form-item-fff">
        <input
          type="password"
          v-model="loginForm.againpassword"
          placeholder="Repeat the password"
        />
      </div>
      <div class="user-name">
        <div class="text">
          <input
            type="tel"
            class="vercode"
            v-model="loginForm.verification_code"
            placeholder="Verification Code"
          />
        </div>
        <div class="send" @click="setCodeEvent">{{ $t("senout") }}</div>
      </div>
      <div class="form-item-fff incode">
        <input
          type="text"
          v-model="loginForm.invatecode"
          placeholder="Invitation code"
          :disabled="isCode"
        />
        <div class="optional">Optional</div>
      </div>
      <button @click="confirmSubmit">Sign up</button>
    </div>
    <p class="forget-pass" @click="toLogin">Log in</p>
  </div>
</template>

<script>
import { countDwon, getQueryVariable } from "@/utils/common";
import { Toast } from "vant";
export default {
  data() {
    return {
      loginForm: {
        password: "",
        phone: "",
        againpassword: "",
        verification_code: "",
        verification_key: "",
        invatecode: "",
      },
      isCode: false,
    };
  },
  mounted() {
    const code = getQueryVariable("code");
    if (code) {
      this.loginForm.invatecode = code;
      this.isCode = true;
    }
  },
  methods: {
    confirmSubmit() {
      if (this.loginForm["phone"] == "") {
        this.$toast("Please enter the phone number");
        return;
      }
      if (this.loginForm["password"] == "") {
        this.$toast("Please enter the password");
        return;
      }
      if (this.loginForm["againpassword"] == "") {
        this.$toast("Please enter the password again");
        return;
      }
      if (this.loginForm["password"] != this.loginForm["againpassword"]) {
        this.$toast("The two passwords are inconsistent");
        return;
      }
      if (this.loginForm["verification_code"] == "") {
        this.$toast("Please enter the invitation code");
        return;
      }
      this.show = true;
      let data = {
        phone: this.loginForm["phone"],
        password: this.loginForm["password"],
        invite_code: this.loginForm["invatecode"],
        verification_key: this.loginForm["verification_key"],
        verification_code: this.loginForm["verification_code"],
      };
      this.register(data);
    },
    register(data) {
      const loginData = {
         name: this.loginForm["phone"],
        password: this.loginForm["password"],
      }
      this.$store.dispatch("registers", data).then((res) => {
        res == true &&
          this.$toast("Register success") &&
          this.login(loginData)
      });
    },
    // 获取验证码
    setCodeEvent(e) {
      if (this.loginForm["phone"] != "") {
        this.$store
          .dispatch("sendcode", {
            phone: this.loginForm.phone,
            area: 91,
            type: "register",
            captcha_key: "1",
            captcha_code: this.invatecode,
          })
          .then((result) => {
            countDwon(e.target, 60);
            this.loginForm.verification_key = result;
            Toast("Send success");
          });
      } else {
        this.$toast("Please enter the phone number");
      }
    },
    // 去登陆
    toLogin() {
      this.$router.push("/login");
    },
    login(data) {
      this.$store.dispatch("login", data).then((res) => {
        res == true &&
          this.$toast("Login successed") &&
          this.$router.replace("/market");
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.register-card {
  height: 100%;
  min-height: 100vh;
  padding: 0 38px;
  background: url("../../assets/imgs/register_bg.png");
  background-size: contain;
  .register-title {
    padding: 70px 0 0 0;
    font-weight: bold;
    font-size: 35px;
    line-height: 35px;
  }
  .register-desc {
    margin: 12px 0 0 0;
    font-size: 12px;
  }
  .register-form {
    margin: 43px 0 0 0;
    font-size: 15px;
    color: #fff;
    .form-item {
      margin: 0 0 10px 0;
      input {
        width: 300px;
        height: 50px;
        box-sizing: border-box;
        border: none;
        background: #f7f7f8;
        color: #adb4c7;
        font-size: 15px;
        padding: 0 10px;
        border-radius: 5px;
      }
    }
    .form-item-fff {
      margin: 0 0 10px 0;
      background-color: #f7f7f8;
      input {
        width: 300px;
        height: 50px;
        background-color: #f7f7f8;
        box-sizing: border-box;
        border: none;
        color: #adb4c7;
        font-size: 15px;
        padding: 0 10px;
        border-radius: 5px;
      }
    }
    .incode {
      display: flex;
      align-items: center;
      justify-content: space-between;
    }
    .optional {
      font-size: 15px;
      color: #adb4c7;
      padding: 0 10px 0 0;
    }
    .form-item p {
      margin: 0 0 12px 0;
    }
    .vercode {
      color: #adb4c7;
    }
    input::-webkit-input-placeholder {
      color: #adb4c7;
    }
    button {
      margin: 20px 0 0 0;
      width: 300px;
      height: 50px;
      background: #4a67f5;
      box-shadow: 0px 2px 6px rgba(45, 88, 234, 0.5);
      opacity: 1;
      border-radius: 25px;
      border: none;
    }
  }
  .user-name {
    margin: 0 0 10px 0;
    display: flex;
    align-items: center;
    background-color: #f7f7f8;
    .text {
      flex: 1;
      display: flex;
      input {
        font-size: 15px;
        height: 50px;
        background-color: #f7f7f8;
        flex: 1;
        border: none;
      }
    }
    .vercode {
      padding: 0 10px;
    }
    .send {
      width: 64px;
      height: 26px;
      font-size: 12px;
      margin: 0 10px 0 0;
      color: #4c6cf1;
      padding: 2px 8px;
      background: #e3e7fe;
      border: 1px solid #4c6cf1;
      opacity: 1;
      border-radius: 20px;
      text-align: center;
      line-height: 26px;
    }
  }
  .forget-pass {
    text-align: center;
    margin: 62px 0 0 0;
    font-size: 15px;
    color: #4a67f5;
  }
  .to-sign {
    margin: 96px 0 0 0;
    text-align: center;
    color: #fff;
    span {
      font-size: 15px;
      color: #2c57e7;
    }
  }
}
</style>